package com.aptemo.webetek.client.widgets.paging;

import com.aptemo.webetek.client.utils.TokenUtils;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PagingView
		extends HorizontalPanel implements PagingDisplay
{
	private static PagingViewUiBinder	uiBinder	= GWT.create(PagingViewUiBinder.class);

	interface PagingViewUiBinder extends UiBinder<Widget, PagingView>
	{
	}

	public interface MyStyle extends CssResource
	{
		String enableLink();

		String disableLink();
	}

	@UiField
	MyStyle		style;

	@UiField
	Anchor		next;

	@UiField
	Anchor		previous;

	@UiField
	Anchor		start;

//	@UiField
//	Anchor		end;

	@UiField
	HTML		status;

	private int	pageNo	= 0;
	private int	lastPage;

	public PagingView()
	{
		add(uiBinder.createAndBindUi(this));
	}

	public void update(int cursor, int step, int size)
	{
		int page = cursor / step;

		this.pageNo = page;
		this.lastPage = size / step;

		int to = cursor + step > size ? size : cursor + step;
		int from = size == 0 ? 0 : cursor + 1;
		status.setText(from + " - " + to + " of " + size);

		// Handle History
		if (page > 0)
		{
			String historyToken = TokenUtils.setToken(History.getToken(), "page", page + "");
			History.newItem(historyToken, false);
		}
		// String historyToken = History.getToken();
		// String tokens[] = historyToken.split("/p");
		// if (tokens.length == 2)
		// {
		// historyToken = tokens[0];
		// // override paging token...
		// History.newItem(historyToken + "/p" + page, false);
		// }

		if (cursor == 0)
		{
			previous.setStyleName(style.disableLink());
			start.setStyleName(style.disableLink());
		}
		else
		{
			previous.removeStyleName(style.disableLink());
			start.removeStyleName(style.disableLink());
		}

		if (cursor >= size - step)
		{
			next.setStyleName(style.disableLink());
			//end.setStyleName(style.disableLink());
		}
		else
		{
			next.removeStyleName(style.disableLink());
			//end.removeStyleName(style.disableLink());
		}

	}

	@UiHandler("next")
	void onNextClick(ClickEvent event)
	{
		applyHistory(pageNo + 1);
	}

	@UiHandler("previous")
	void onPreviousClick(ClickEvent event)
	{
		applyHistory(pageNo - 1);
	}

	@UiHandler("start")
	void onHomeClick(ClickEvent event)
	{
		applyHistory(0);
	}

//	@UiHandler("end")
//	void onEndClick(ClickEvent event)
//	{
//		applyHistory(lastPage);
//	}

	private void applyHistory(int page)
	{
		// String historyToken = History.getToken();
		// String tokens[] = historyToken.split("/p");
		// if (tokens.length == 2)
		// {
		// historyToken = tokens[0];
		// }
		// // if (pageNo > 0)
		// {
		// History.newItem(historyToken + "/p-" + page);
		// }
		String token = History.getToken();
		
//		String tmp = TokenUtils.getTokenPart(token, base);
//		if (tmp.isEmpty())
//		{
//			return;
//		}

		String historyToken = TokenUtils.setToken(token, "page", page + "");
		History.newItem(historyToken);
	}

	@Override
	public HasClickHandlers getPagingNextHandler()
	{
		return next;
	}

	@Override
	public HasClickHandlers getPagingPreviousHandler()
	{
		return previous;
	}

//	@Override
//	public HasClickHandlers getPagingEndHandler()
//	{
//		return end;
//	}

	@Override
	public HasClickHandlers getPagingHomeHandler()
	{
		return start;
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

}
